package com.malty.apimanage.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.malty.apimanage.domain.dto.DeleteRequest;
import com.malty.apimanage.domain.dto.user.*;
import com.malty.apimanage.domain.vo.UserVO;
import com.malty.apimanage.service.UserInfoService;
import com.malty.common.ErrorCode;
import com.malty.common.domain.Result;
import com.malty.common.domain.entity.UserInfo;
import com.malty.common.exception.BusinessException;
import com.malty.common.util.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author malty
 */
@Api(tags = "用户信息")
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserInfoService userService;

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public Result<Long> userRegister(@Validated @RequestBody UserRegisterReq userRegisterReq) {
        long result = userService.userRegister(userRegisterReq);
        return ResultUtils.success(result);
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result<UserInfo> userLogin(@RequestBody UserLoginReq userLoginReq) {
        UserInfo user = userService.userLogin(userLoginReq);
        return ResultUtils.success(user);
    }

    @ApiOperation("用户注销")
    @PostMapping("/logout")
    public Result<Boolean> userLogout(HttpServletRequest request) {
        boolean result = userService.userLogout(request);
        return ResultUtils.success(result);
    }

    @ApiOperation("获取登录用户信息")
    @GetMapping("/get/login")
    public Result<UserVO> getLoginUser(HttpServletRequest request) {
        UserVO user = userService.getLoginUser(request);
        return ResultUtils.success(user);
    }

    @ApiOperation("创建用户")
    @PostMapping("/add")
    public Result<Long> addUser(@RequestBody UserAddReq userAddRequest) {
        UserInfo userInfo = userService.saveUser(userAddRequest);
        return ResultUtils.success(userInfo.getId());
    }

    @ApiOperation("删除用户")
    @PostMapping("/delete")
    public Result<Boolean> deleteUser(@RequestBody DeleteRequest deleteRequest) {
        boolean b = userService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }

    @ApiOperation("更新用户")
    @PostMapping("/update")
    public Result<Boolean> updateUser(@RequestBody UserUpdateReq userUpdateRequest) {
        boolean result = userService.updateUserById(userUpdateRequest);
        return ResultUtils.success(result);
    }

    @ApiOperation("根据id查询用户")
    @GetMapping("/get")
    public Result<UserVO> getUserById(Long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserVO userVO = userService.getUserById(id);
        return ResultUtils.success(userVO);
    }

    @ApiOperation("根据条件用户")
    @GetMapping("/list")
    public Result<List<UserVO>> listUser(UserQueryReq userQueryReq) {
        List<UserVO> userList = userService.listUserByQuery(userQueryReq);
        return ResultUtils.success(userList);
    }


    @ApiOperation("根据条件分页查询用户")
    @GetMapping("/list/page")
    public Result<Page<UserVO>> listUserByPage(UserQueryReq userQueryReq) {
        Page<UserVO> userPage = userService.pageList(userQueryReq);
        return ResultUtils.success(userPage);
    }
}
